/**
 * 
 */
package operands;

import java.util.StringTokenizer;

import javax.swing.JOptionPane;

import implementation.Node;

/**
 * <br><b>Operand</b>s specific type of nod that represents the tree's leafs</br>
 * <b>Extends</b> Node
 * @author Tarca Alexandra
 * @version 1.6
 * @since 30/4/2011
 */
public class Operand extends Node {

	/**
	 * @param val - the string representation of the operation
	 */
	public Operand(String val) {
		super(val);
	}

	/**
	 * @param  void
	 * @return Double representation of the derived token contained in Node.content
	 */
	
	public Double computeDerivate() {
		if(getContent().contains("sh")){
			StringTokenizer st = new StringTokenizer(getContent(),"sh()");
			String val = st.nextToken();                                                                                        
			return (Math.pow(Math.E,Integer.parseInt(val))	+
									  (Math.pow(Math.E,0-Integer.parseInt(val)))	
												)/2;
		}
		if(getContent().contains("sqrt")){
			StringTokenizer st = new StringTokenizer(getContent(),"sqrt()");
			String val = st.nextToken();		
			return Math.pow(Double.parseDouble(val),0.5);		                        										
		}
		if(getContent().contains("arcsin")){
			StringTokenizer st = new StringTokenizer(getContent(),"arcsin()");
			String val = st.nextToken();
			return 1/Math.sqrt(Math.abs(1-Double.parseDouble(val)*Double.parseDouble(val)));
		}
		if(getContent().contains("arccos")){
			StringTokenizer st = new StringTokenizer(getContent(),"arccos()");
			String val = st.nextToken();
			return -1/Math.sqrt(Math.abs(1-Double.parseDouble(val)*Double.parseDouble(val)));
		}
		if(getContent().contains("arcctg")){
			StringTokenizer st = new StringTokenizer(getContent(),"arcctg()");
			String val = st.nextToken();
			return -1/(1+Double.parseDouble(val)*Double.parseDouble(val));
		}
		if(getContent().contains("arctg")){
			StringTokenizer st = new StringTokenizer(getContent(),"arctg()");
			String val = st.nextToken();
			return 1/(1+Double.parseDouble(val)*Double.parseDouble(val));
		}
		if(getContent().contains("sin")){
			StringTokenizer st = new StringTokenizer(getContent(),"sin()");
			return Math.cos(Double.parseDouble(st.nextToken()));
		}
		if(getContent().contains("cos")){
			StringTokenizer st = new StringTokenizer(getContent(),"cos()");
			String val = st.nextToken();
			return -Math.sin(Double.parseDouble(val));
		}
		if(getContent().contains("ctg")){
			StringTokenizer st = new StringTokenizer(getContent(),"ctg()");
			String val = st.nextToken();
			return -1/(Math.sin(Double.parseDouble(val))*Math.sin(Double.parseDouble(val)));
		}
		if(getContent().contains("tg")){
			StringTokenizer st = new StringTokenizer(getContent(),"tg()");
			return 1+Math.tan(Double.parseDouble(st.nextToken()))*Math.tan(Double.parseDouble(st.nextToken()));
		}
		try {
			System.out.println(Double.parseDouble(getContent()));
		}
		catch (Exception e){
			JOptionPane.showMessageDialog(null, e.getMessage());
			System.exit(1);
		}
		return 0.0;
		
	}

	/**
	 * @param  void
	 * @return String representation of the derived token contained in Node.content
	 */
	public String evalDerivate() {
		
		if(getContent().contains("sqrt")){
			StringTokenizer st = new StringTokenizer(getContent(),"sqrt()");
			String val = st.nextToken();
			return val+"^0.5";
		}

		if(getContent().contains("arcsin")){
			StringTokenizer st = new StringTokenizer(getContent(),"arcsin()");
			String val = st.nextToken();
			return "1/sqrt(1-"+val+"*"+val+")";
		}
		if(getContent().contains("arccos")){
			StringTokenizer st = new StringTokenizer(getContent(),"arccos()");
			String val = st.nextToken();
			return "-1/sqrt(1-"+val+"*"+val+")";
		}
		if(getContent().contains("arcctg")){
			StringTokenizer st = new StringTokenizer(getContent(),"arcctg()");
			String val = st.nextToken();
			return "-1/(1+"+val+"*"+val+")";
		}
		if(getContent().contains("arctg")){
			StringTokenizer st = new StringTokenizer(getContent(),"arctg()");
			String val = st.nextToken();
			return "1/(1+"+val+"*"+val+")";
		}
		if(getContent().contains("sin")){
			StringTokenizer st = new StringTokenizer(getContent(),"sin()");
			String val = st.nextToken();
			return "cos("+val+")";
		}
		if(getContent().contains("cos")){
			StringTokenizer st = new StringTokenizer(getContent(),"cos()");
			String val = st.nextToken();
			return "-sin("+val+")";
		}
		if(getContent().contains("ctg")){
			StringTokenizer st = new StringTokenizer(getContent(),"ctg()");
			String val = st.nextToken();
			return "-1/(sin("+val+")*sin("+val+"))";
		}
		if(getContent().contains("tg")){
			StringTokenizer st = new StringTokenizer(getContent(),"tg()");
			String val = st.nextToken();
			return "1+tg("+val+")*tg("+val+")";
		}
		if(getContent().contains("x")||getContent().contains("X") )
			return "1";
		return "0";
	}
}
